package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.acticle.domain.Destination;
import cn.wolfcode.luowowo.acticle.domain.StrategyContent;
import cn.wolfcode.luowowo.acticle.domain.StrategyDetail;
import cn.wolfcode.luowowo.acticle.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.acticle.service.*;
import cn.wolfcode.luowowo.acticle.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.prefix.StrategyStatisKeyPrefix;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyContentService strategyContentService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;


    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategySearchService strategySearchService;


    @RequestMapping
    public String index(Model model) {

        StrategySearchQuery query = new StrategySearchQuery();
        query.setPageSize(10);

        //所有的推荐排行数据
        List<StrategyStatisVO> all = strategyStatisRedisService.getAllCommends();

        //海内,外攻略推荐排序(点赞数+收藏数)， 默认前10
        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();
        for (StrategyStatisVO vo : all) {
            if (vo.isIsabroad()) {
                if (abroadCds.size() < 10) {
                    abroadCds.add(vo);
                }
            } else {
                if (unabroadCds.size() < 10) {
                    unabroadCds.add(vo);
                }
            }
        }

        //国内攻略推荐排序(点赞数+收藏数)， 默认前10
        model.addAttribute("unabroadCds", unabroadCds);
        model.addAttribute("abroadCds", abroadCds);

        //热门攻略推荐排行(浏览数+评论数)， 默认前10
        List<StrategyStatisVO> hotCds = strategyStatisRedisService.getHotCommends();
        model.addAttribute("hotCds", hotCds);


        //主题推荐排行 list<map>
        List<Map<String, Object>> themeCds = strategySearchService.queryThemeCommends();
        model.addAttribute("themeCds", themeCds);


        //查询条件
        //海外   abroads
        List<StatisVO> abroads = strategySearchService.queryStrategyCondition(SearchQueryObject.CONDITION_TYPR_ABROAD);
        model.addAttribute("abroads", abroads);
        //国内   chinas
        List<StatisVO> chinas = strategySearchService.queryStrategyCondition(SearchQueryObject.CONDITION_TYPR_UN_ABROAD);
        model.addAttribute("chinas", chinas);

        //主题   themes
        List<StatisVO> themes = strategySearchService.queryStrategyCondition(SearchQueryObject.CONDITION_TYPR_THEME);
        model.addAttribute("themes", themes);


        //攻略推荐， 默认前5
        model.addAttribute("commends", strategyCommendService.getHotStrategyCommendTopN(5));


        return "strategy/search";
    }


    //分页页面
    @RequestMapping("/searchPage")
    public String strategySearchPage(Model model, @ModelAttribute("qo") StrategySearchQuery qo){
        model.addAttribute("page", strategySearchService.query(qo));
        return "strategy/searchPageTpl";
    }

    @RequestMapping("/detail")
    public String detail(Model model, Long id, @UserParam UserInfo userInfo) {

        //统计排行， 这里跟redis一定程度上算重复了
        //strategySearchService.statisNumIncrease(id, "viewnum", 1);


        model.addAttribute("vo", strategyStatisRedisService.getStatisVo(id));

        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyContentService.get(id);
        detail.setStrategyContent(content);
        model.addAttribute("detail", detail);
        if (userInfo != null) {
            model.addAttribute("isFavor", strategyStatisRedisService.isFavor(StrategyStatisKeyPrefix.STRATEGY_STATIS_FAVOR, id, userInfo.getId()));
        }
        //添加查看次数 +1
        strategyStatisRedisService.viewIncrease(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO, id);
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT, id, 1);

        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {

        List<Destination> toasts = destinationService.getToastById(qo.getDestId());
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
        model.addAttribute("toasts", toasts);
        model.addAttribute("pageInfo", strategyDetailService.query(qo));

        model.addAttribute("tags", strategyTagService.list());

        return "strategy/list";
    }

    //攻略点赞thumbUp
    @RequestMapping("strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            return new AjaxResult("请先登录", 102);
        }
        boolean isT = strategyStatisRedisService.thumbupIncrease(StrategyStatisKeyPrefix.STRATEGY_STATIS_THUMBUP, sid, userInfo.getId());
        if (isT) {
            strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT, sid, 1);
        }
//         strategySearchService.statisNumIncrease(sid, "thumbupnum", 1);
        return new AjaxResult(isT, "")
                .addData(strategyStatisRedisService.getStatisVo(sid));
    }


    //用户评论
    @RequireLogin
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(Model model, StrategyComment comment, @UserParam UserInfo userInfo) {
        comment.setUserId(userInfo.getId());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setUsername(userInfo.getNickname());
        strategyCommentService.save(comment);
        strategyStatisRedisService.replyIncrease(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO, comment.getDetailId());
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT, comment.getDetailId(), 1);
        return new AjaxResult().addData(strategyStatisRedisService.getStatisVo(comment.getDetailId()).getReplynum());
    }

    //分页模板
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo) {
        model.addAttribute("page", strategyCommentService.query(qo));
        return "strategy/commentTpl";
    }

    //评论点赞thumbUp
    @RequestMapping("commentThumbUp")
    @ResponseBody
    @RequireLogin
    public Object thumbup(String toid, Long fromid, @UserParam UserInfo userInfo) {
        boolean ret = strategyCommentService.thumbUp(toid, fromid);
        return new AjaxResult(ret, "");
    }

    //收藏favor
    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            return new AjaxResult("请先登录", 102);
        }

        boolean ret = strategyStatisRedisService.favor(StrategyStatisKeyPrefix.STRATEGY_STATIS_FAVOR, sid, userInfo.getId());
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT, sid, ret ? 1 : -1);
        return new AjaxResult(ret, "")
                .addData(strategyStatisRedisService.getStatisVo(sid));
    }
    //分享
    @RequestMapping("/share")
    @ResponseBody
    // @RequireLogin
    public Object share(Long sid) {

        //分享数量 分享一次加1
        strategyStatisRedisService.shareIncrease(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO, sid);
        //分享排行
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SHARE, sid, 1);
        // 分享时 分数+1
        return new AjaxResult("分享成功").addData(strategyStatisRedisService.getStatisVo(sid));
    }

}
